using System;

namespace Runtime.Common.Utility
{
    public static class TypeEx
    {
        public static bool IsInheritFrom(this Type t, Type parent)
        {
            return parent.IsSubclassOf(t);
        }

        public static Type GetInheritInterface(this Type t, string name, bool isRecursion, bool ignoreCase = false)
        {
            Type type = t.GetInterface(name);
            return type;
        }

        public static Type GetInheritInterface<T>(this Type t, bool isRecursion, bool ignoreCase)
        {
            return t.GetInheritInterface(typeof(T), isRecursion, ignoreCase);
        }

        public static Type GetInheritInterface(this Type t, Type inheritType, bool isRecursion, bool ignoreCase)
        {
            return t.GetInheritInterface(inheritType.FullName, isRecursion, ignoreCase);
        }

        /// <summary>
        /// 是否继承自baseClass
        /// </summary>
        /// <param name="t"></param>
        /// <param name="baseClass"></param>
        /// <param name="isOneSelf">是否包含baseClass</param>
        /// <returns></returns>
        public static bool ISDescendFrom(this Type t, Type baseClass, bool isOneSelf = false)
        {
            if (!isOneSelf)
            {
                if (t == baseClass)
                {
                    return false;
                }
            }

            return baseClass.IsAssignableFrom(t);
        }
    }
}